<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyXuyuan as CompanyXuyuanModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\CompanyVow as CompanyVowModel;
/**
 * 许愿控制器
 * Class Companyxuyuan
 * @package app\home\controller
 */
class Companyxuyuan extends Base
{
    protected $moren = [
        'name' => '祈福',
        'intro' => '弟子愿向菩萨诚心祈祷，惟愿',
        'standard' =>array(
            [
                'name' => '释迦摩尼佛',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/1-%E9%87%8A%E4%BC%BD%E7%89%9F%E5%B0%BC%E4%BD%9B.png',
            ],[
                'name' => '阿弥陀佛',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/2-%E9%98%BF%E5%BC%A5%E9%99%80%E4%BD%9B.png',
            ],[
                'name' => '药师佛',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/3-%E8%8D%AF%E5%B8%88%E4%BD%9B.png',
            ],[
                'name' => '弥勒佛',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/4-%E5%BC%A5%E5%8B%92%E4%BD%9B.png',
            ],[
                'name' => '大势至菩萨',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/5-%E5%A4%A7%E5%8A%BF%E8%87%B3%E8%8F%A9%E8%90%A8.png',
            ],[
                'name' => '观音菩萨',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/6-%E8%A7%82%E9%9F%B3%E8%8F%A9%E8%90%A8.png',
            ],[
                'name' => '地藏菩萨',
                'img' => 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E4%BD%9B%E5%83%8F/7-%E5%9C%B0%E8%97%8F%E8%8F%A9%E8%90%A8.png',
            ]
        )
    ];
    /**
     * [index  获取许愿参数]
     * @param
     * @author [C。]
     * @return json
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyXuyuanModel;
        try{
            $data = $model->findData($param['id'],'comid','standard,name,intro');
            if(empty($data)){
                $data = $this->moren;
            }else{
                $data['standard'] = unserialize($data['standard']);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [index  许愿]
     * @param
     * @author [C。]
     * @return json
     */
    public function add()
    {
        $param = input('param.');
        $model = new CompanyVowModel;
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $id = $model->addData($param);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '许愿失败' . $e->getMessage());
        }
        return admin_json(1, '许愿成功',$id);
    }
    /**
     * [getorder  获取订单查询许愿内容]
     * @param
     * @author [C。]
     * @return json
     */
    public function getorder()
    {
        $param = input('param.');
        $model = new CompanyVowModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $vow = $model->findData($param['id'],'id','content,img');
            $order = $cmodel->findOne(['vid'=>$param['id'],'type'=>7],'gongdeng,day,name,to_name,content,create_time,vid,status');
            $huanyuan = $cmodel->findOne(['vid'=>$param['id'],'type'=>6,'status'=>1]);
            if(empty($huanyuan)){
                $vow['is_huan'] = 0;
            }else{
                $vow['is_huan'] = 1;
            }
            if(!empty($order)){
                if($order['status'] == 1){
                    $vow['order'] = $order;
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$vow);
    }
    /**
     * [getlist  获取许愿清单]
     * @param
     * @author [C。]
     * @return json
     */
    public function getlist()
    {
        $param = input('param.');
        $model = new CompanyVowModel;
        $cmodel = new CompanyGongdengOrderModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            $map['mid'] = $user['id'];
            $map['comid'] = $param['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,'id,content,create_time');
            foreach($lists as &$v){
                $order1 = $cmodel->findOne(['vid'=>$v['id'],'type'=>7,'status'=>1],'id,bless');
                if(empty($order1)){
                    $v['is_diandeng'] = 0;
                }else{
                    $v['is_diandeng'] = 1;
                }
                $v['bless'] = $order1['bless'];
                $order2 = $cmodel->findOne(['vid'=>$v['id'],'type'=>6,'status'=>1],'id');
                if(empty($order2)){
                    $v['is_huanyuan'] = 0;
                }else{
                    $v['is_huanyuan'] = 1;
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [donornum  捐款人数]
     * @param
     * @author [C。]
     * @return json
     */
    public function donornum()
    {
        $param = input('param.');
        $model = new CompanyXuyuanModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','donor_num');
            if(empty($data)){
                $data['donor_num'] = 0;
            }
            $data['really'] = $cmodel->getAllCount(['type'=>3,'status'=>1,'comid'=>$param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}